package cn.changgou.goods.controller;

import cn.changgou.common.pojo.PageResult;
import cn.changgou.common.pojo.Result;
import cn.changgou.common.pojo.StatusCode;
import cn.changgou.goods.pojo.Brand;
import cn.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/brand")
@RestController
@Api(tags = "品牌管理接口api")//标签专门用来标注类的
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/findAll")
    @ApiOperation("查询所有品牌")
    public Result<List<Brand>> findAll() {
        List<Brand> brandList = brandService.findALl();
        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询品牌")
    public Result findById(@PathVariable Integer id) {
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", brand);
    }

    @PostMapping("/add")
    @ApiOperation("添加品牌")
    public Result addBrand(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    @PutMapping("/update")
    @ApiOperation("修改品牌")
    public Result updateBrand(@RequestBody Brand brand) {
        brandService.updateBrand(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除品牌")
    public Result deleteBrand(@PathVariable Integer id) {
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @GetMapping("/search")
    @ApiOperation("多条件搜索品牌")
    public Result search(@RequestParam Map searchMap) {
        List brandList = brandService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping("/findPage/{page}/{size}")
    @ApiOperation("分页查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", required = true),
                    @ApiImplicitParam(name = "size", value = "条数", defaultValue = "10", required = true)
            }

    )
    public PageResult findPage(@PathVariable Integer page,
                               @PathVariable Integer size) {
        Page<Brand> brands = brandService.findPage(page, size);
        long total = brands.getTotal();
        List<Brand> rows = brands.getResult();
        return new PageResult(total, rows);
    }

    @GetMapping("/findPageSearch/{page}/{size}")
    @ApiOperation("多条件分页查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", required = true),
                    @ApiImplicitParam(name = "size", value = "条数", defaultValue = "10", required = true)
            }

    )
    public PageResult findPage(@PathVariable Integer page,
                               @PathVariable Integer size,
                               Brand brand) {
        Page<Brand> brands = brandService.findPage(page, size, brand);
        long total = brands.getTotal();
        List<Brand> rows = brands.getResult();
        return new PageResult(total, rows);
    }
}
